package com.yunfeng.supercall.activity;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;

import com.ly.quickdev.library.adapter.ListBaseAdapter;
import com.ly.quickdev.library.utils.JsonUtils;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.bean.ContactsBaseBean;
import com.yunfeng.supercall.bean.MyCircleBean;
import com.yunfeng.supercall.bean.SuperFriendsBean;
import com.yunfeng.supercall.httputils.MyAjaxCallBack;
import com.yunfeng.supercall.manager.ManagerFactory;

/**
 * 我的圈子详情
 * 
 * @author 洋
 * 
 */
public class MyCircleDetailActivity extends BaseActivity {

	private List<SuperFriendsBean> mFriends;
	private MyCircleBean mCircle;
	private GridView mGridView;
	private Button mButton;
	List<MyCircleBean> circles;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mycircle_detail);
		mCircle = getIntent().getParcelableExtra(KEY_DATA);
		mButton = findView(R.id.handle);
		circles = (List<MyCircleBean>) ManagerFactory.createManager(MyCircleBean.class)
				.getList();
		if (circles.contains(mCircle)) {
			mButton.setText(R.string.out_circle);
		} else {
			mButton.setText(R.string.in_circle);
		}
		loadPeople();
		mGridView = findView(R.id.gridview);
		setTextViewText(R.id.addr, mCircle.getAddress());
		setTextViewText(R.id.date, mCircle.getCreatetime());
		setTextViewText(R.id.jianjie, mCircle.getNote());
		setTitleBar(mCircle.getName());
		mGridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				List<SuperFriendsBean> mSuperFriends = (List<SuperFriendsBean>) ManagerFactory
						.createManager(SuperFriendsBean.class).getList();
				SuperFriendsBean friends = mFriends.get(position);
				startActivity(new Intent(getActivity(),
						FriendsDetailActivity.class).putExtra(KEY_DATA,
						mFriends.get(position)));
			}
		});
	}

	public void quite(View v) {
		if (circles.contains(mCircle)) {

			JSONObject json = getBaseParams(getActivity());
			try {
				json.put("i_circleid", mCircle.getCircleid());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			showProgressDialog(R.string.quiting);
			mHttpClient.post(FUNCTION_OUT_CIRCLE, json, new MyAjaxCallBack() {

				@Override
				public void onReceiveError(String msg, String errorCode) {
					// TODO Auto-generated method stub
					showToast(msg);
					cancelProgressDialog();
				}

				@Override
				public void onReceiveData(String data, String msg) {
					// TODO Auto-generated method stub
					cancelProgressDialog();
					MyCircleBean circle = null;
					for (MyCircleBean m : circles) {
						if (m.getCircleid() == mCircle.getCircleid()) {
							circle = m;
						}
					}
					circles.remove(circle);
					showToast(R.string.quit_circle_success);
					finish();
				}
			});
		} else {
			JSONObject json = getBaseParams(getActivity());
			try {
				json.put("i_circleid", mCircle.getCircleid());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			showProgressDialog(R.string.joining);
			mHttpClient.post(FUNCTION_JOIN_OTHER_CIRCLE, json,
					new MyAjaxCallBack() {

						@Override
						public void onReceiveError(String msg, String errorCode) {
							// TODO Auto-generated method stub
							showToast(msg);
							cancelProgressDialog();
						}

						@Override
						public void onReceiveData(String data, String msg) {
							// TODO Auto-generated method stub
							cancelProgressDialog();
							circles.add(mCircle);
							showToast(R.string.join_circle_success);
							mButton.setText(R.string.out_circle);
						}
					});
		}
	}

	private void loadPeople() {
		JSONObject json = getBaseParams(getActivity());
		try {
			json.put("i_circleid", mCircle.getCircleid());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mHttpClient.post(FUNCTION_GET_CIRCLE_USER, json, new MyAjaxCallBack() {

			@Override
			public void onReceiveError(String msg, String errorCode) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onReceiveData(String data, String msg) {
				// TODO Auto-generated method stub
				mFriends = JsonUtils.parseList(data, SuperFriendsBean.class);
				mGridView.setAdapter(new GridAdapter(getActivity(), mFriends));
			}
		});
	}

	class GridAdapter extends ListBaseAdapter<SuperFriendsBean> {

		public GridAdapter(Context context, List<SuperFriendsBean> list) {
			super(context, list);
			// TODO Auto-generated constructor stub
		}

		@Override
		public View initView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(
						R.layout.grid_item_circle_user, null);
			}
			ContactsBaseBean.setupSingleLineContactsView(convertView,
					getItem(position));
			return convertView;
		}

	}
}
